package com.cennavi.minemap.controller;

import com.alibaba.fastjson.JSONObject;
import com.cennavi.minemap.bean.MessageBean;
import com.cennavi.minemap.service.BaiduService;
import com.cennavi.minemap.service.MapbarService;
import com.cennavi.minemap.service.WEBTService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@ComponentScan(basePackages = {"com.cennavi.minemap.service"})
@Api(value = "Search controller", description = "Search controller")
@RequestMapping("/service")
public class SearchController {

    @Autowired
    private WEBTService searchService;
    @Autowired
    private BaiduService bsearchService;
    @Autowired
    private MapbarService mapbarService;

    @RequestMapping(value = {"/search2/keyword", "/search/keyword"})
    public MessageBean keyword(@RequestParam("token") String token,
                               @RequestParam(value = "adCode", defaultValue = "") String adCode, @RequestParam("key") String key,
                               @RequestParam(value = "searchType", defaultValue = "all") String searchType,
                               @RequestParam(value = "dataType", defaultValue = "") String dataType,
                               @RequestParam(value = "pageNumber", defaultValue = "1") Integer pageNumber,
                               @RequestParam(value = "pageCount", defaultValue = "10") Integer pageCount,
                               @RequestParam(value = "source", defaultValue = "1") Integer source) {

        MessageBean messageBean = new MessageBean();

        String response;

        try {
            if (source == 3) {
                response = mapbarService.keyword(adCode, key, pageNumber, pageCount);
            } else if (source == 2) {
                response = bsearchService.keyword(key, adCode, pageNumber, pageCount);
            } else {
                response = searchService.keyword(token, adCode, key, searchType, dataType, pageNumber, pageCount);
            }
            JSONObject resJson = JSONObject.parseObject(response);

            messageBean.setErrcode(0);
            messageBean.setErrmsg(null);
            messageBean.setData(resJson);

        } catch (Exception e) {
            e.printStackTrace();
            messageBean.setErrcode(-1);
            messageBean.setErrmsg("keyword error");
        }
        return messageBean;
    }

    @RequestMapping(value = {"/search/surround", "/search2/surround"})
    public MessageBean surround(@RequestParam("token") String token,
                                @RequestParam(value = "adCode", defaultValue = "") String adCode, @RequestParam("key") String key,
                                @RequestParam(value = "searchType", defaultValue = "all") String searchType,
                                @RequestParam(value = "dataType", defaultValue = "") String dataType,
                                @RequestParam(value = "pageNumber", defaultValue = "1") Integer pageNumber,
                                @RequestParam(value = "pageCount", defaultValue = "10") Integer pageCount,
                                @RequestParam(value = "source", defaultValue = "1") Integer source,
                                @RequestParam(value = "location") String location,
                                @RequestParam(value = "inRadius", defaultValue = "0") Integer inRadius,
                                @RequestParam(value = "spatialFunction", defaultValue = "geofilt") String spatialFunction,
                                @RequestParam(value = "radius", defaultValue = "500") Integer radius,
                                @RequestParam(value = "seq", defaultValue = "0") Integer seq) {

        MessageBean messageBean = new MessageBean();

        double lon = Double.parseDouble(location.split(",")[0]);
        double lat = Double.parseDouble(location.split(",")[1]);

        String response;

        try {
            if (source == 3) {
                response = mapbarService.surround(key, pageNumber, pageCount, location, radius, adCode);
            } else if (source == 2) {
                response = bsearchService.surround(key, pageNumber, pageCount, location, radius);
            } else {
                response = searchService.surround(token, adCode, key, searchType, dataType, pageNumber, pageCount,
                        source, lon, lat, inRadius, spatialFunction, radius, seq);
            }
            JSONObject resJson = JSONObject.parseObject(response);

            messageBean.setErrcode(0);
            messageBean.setErrmsg(null);
            messageBean.setData(resJson);

        } catch (Exception e) {
            e.printStackTrace();
            messageBean.setErrcode(-1);
            messageBean.setErrmsg("surround error");
        }
        return messageBean;
    }

    @RequestMapping(value = {"/search/bbox", "/search2/bbox"})
    public MessageBean bbox(@RequestParam("token") String token,
                            @RequestParam(value = "adCode", defaultValue = "") String adCode, @RequestParam("key") String key,
                            @RequestParam("bounds") String bounds,
                            @RequestParam(value = "searchType", defaultValue = "all") String searchType,
                            @RequestParam(value = "dataType", defaultValue = "") String dataType,
                            @RequestParam(value = "pageNumber", defaultValue = "1") Integer pageNumber,
                            @RequestParam(value = "pageCount", defaultValue = "10") Integer pageCount,
                            @RequestParam(value = "source", defaultValue = "1") Integer source,
                            @RequestParam(value = "seq", defaultValue = "0") Integer seq) {

        MessageBean messageBean = new MessageBean();

        double lon1 = Double.parseDouble(bounds.split(",")[0]);
        double lat1 = Double.parseDouble(bounds.split(",")[1]);
        double lon2 = Double.parseDouble(bounds.split(",")[2]);
        double lat2 = Double.parseDouble(bounds.split(",")[3]);

        String response;

        try {
            if (source == 3) {
                response = mapbarService.bbox(adCode, key, pageNumber, pageCount, lon1, lat1, lon2, lat2);
            } else if (source == 2) {
                response = bsearchService.bbox(key, pageNumber, pageCount, bounds);
            } else {
                response = searchService.bbox(token, adCode, key, searchType, dataType, pageNumber, pageCount, lon1,
                        lat1, lon2, lat2, seq);
            }
            JSONObject resJson = JSONObject.parseObject(response);

            messageBean.setErrcode(0);
            messageBean.setErrmsg(null);
            messageBean.setData(resJson);

        } catch (Exception e) {
            e.printStackTrace();
            messageBean.setErrcode(-1);
            messageBean.setErrmsg("bbox error");
        }
        return messageBean;
    }

    @RequestMapping(value = {"/route/driving", "/route2/driving"})
    public MessageBean driving(@RequestParam("token") String token, @RequestParam("sPoint") String sPoint,
                               @RequestParam("ePoint") String ePoint,
                               @RequestParam(value = "wayPoints", defaultValue = "") String wayPoints,
                               @RequestParam(value = "shpFlag", defaultValue = "1") Integer shpFlag,
                               @RequestParam(value = "costModel", defaultValue = "16") Integer costModel,
                               @RequestParam(value = "vehicleSpeed", defaultValue = "0") Integer vehicleSpeed,
                               @RequestParam(value = "numAlt", defaultValue = "1") Integer numAlt,
                               @RequestParam(value = "criteria", defaultValue = "0") Integer criteria,
                               @RequestParam(value = "source", defaultValue = "1") String source,
                               @RequestParam(value = "traffic", defaultValue = "0") Integer traffic) {

        MessageBean messageBean = new MessageBean();

        String response;

        try {
            if (source.startsWith("3")) {
                // style
                // 0:最快路径
                // 1:最短路径
                // 3:行人模式
                // 4:图吧推荐(默认)
                // 5:最经济
                String style = null;
                if (costModel == 16)
                    style = "0";
                else if (costModel == 32)
                    style = "1";
                else if (costModel == 64)
                    style = "5";
                else
                    style = "4";

                response = mapbarService.driving(sPoint, ePoint, style, source);
            } else {
                // costModel
                // 1 考虑动态交通的最快。
                // 2 考虑动态交通的最短距离
                // 4 考虑动态交通的最经济
                // 8 绿色模式
                // 16 不考虑动态交通的最快
                // 32 不考虑动态交通的最短距离
                // 64 most economical
                response = searchService.driving(token, sPoint, ePoint, wayPoints, shpFlag, costModel, vehicleSpeed,
                        numAlt, criteria, traffic);
            }
            // response = bsearchService.driving(sPoint, ePoint, wayPoints,
            // costModel, criteria);
            JSONObject resJson = JSONObject.parseObject(response);
            messageBean.setErrcode(0);
            messageBean.setErrmsg(null);
            messageBean.setData(resJson);

        } catch (Exception e) {
            e.printStackTrace();
            messageBean.setErrcode(-1);
            messageBean.setErrmsg("driving error");
        }
        return messageBean;
    }

    /**
     * 路线规划
     * <p>
     * 新接口{@link SearchController#newDriving2(java.lang.String, java.lang.String, java.lang.Integer, java.lang.String, java.lang.String)}
     */
    // http://localhost:5551/service/route/driving2?token=25cc55a69ea7422182d00d6b7c0ffa93&source=1&sPoint=116.35983,39.91667&ePoint=116.43177,39.90494&st=4
    @Deprecated
    @RequestMapping(value = {"/route/driving2", "/route2/driving2"})
    public MessageBean newDriving(@RequestParam("token") String token,
                                  @RequestParam(value = "source", defaultValue = "3") String source,
                                  @RequestParam("sPoint") String sPoint,
                                  @RequestParam("ePoint") String ePoint,
                                  @RequestParam(value = "wayPoints", defaultValue = "") String wayPoints,
                                  @RequestParam(value = "st", defaultValue = "4") Integer st,
                                  @RequestParam(value = "avoidArea", defaultValue = "") String avoidArea) {

        MessageBean messageBean = new MessageBean();

        String response;

        try {
            /**
             * st: 0 最快 1 最短 2 规避拥堵 4 默认推荐 5 经济
             */
            response = mapbarService.newDriving(sPoint, ePoint, wayPoints, st, avoidArea);
            JSONObject resJson = JSONObject.parseObject(response);
            messageBean.setErrcode(0);
            messageBean.setErrmsg(null);
            messageBean.setData(resJson);

        } catch (Exception e) {
            e.printStackTrace();
            messageBean.setErrcode(-1);
            messageBean.setErrmsg("driving error");
        }
        return messageBean;
    }

    /**
     * 公交换乘-经纬度查询
     * <p>
     * 新接口{@link SearchController#busTransfer2(java.lang.String, java.lang.String, java.lang.String, java.lang.Integer)}
     */
    // http://localhost:5551/service/route/driving2?token=25cc55a69ea7422182d00d6b7c0ffa93&source=1&sPoint=116.35983,39.91667&ePoint=116.43177,39.90494&st=4
    @Deprecated
    @RequestMapping(value = {"/route/busTransfer", "/route2/busTransfer"})
    public MessageBean busTransfer(@RequestParam("token") String token,
                                   @RequestParam(value = "adCode", defaultValue = "") String adCode,
                                   @RequestParam(value = "strategy", defaultValue = "0") Integer strategy,
                                   @RequestParam(value = "shpInfoFlag", defaultValue = "1") Integer shpInfoFlag,
                                   @RequestParam("sPoint") String sPoint, @RequestParam("ePoint") String ePoint,
                                   @RequestParam(value = "showWalkInfo", defaultValue = "0") Integer showWalkInfo,
                                   @RequestParam(value = "source", defaultValue = "1") Integer source) {

        MessageBean messageBean = new MessageBean();

        String response;

        try {
            if (source == 3) {
                String sort = "subway";
                if (adCode != null && !"".equals(adCode)) {
                    if (strategy == 0) {
                        sort = "subway";
                    } else if (strategy == 1) {
                        sort = "nonstop";
                    } else if (strategy == 2) {
                        sort = "subway";
                    } else {
                        sort = "shortWalk";
                    }

                    String city = searchService.getAdNameByAdCode(adCode);
                    response = mapbarService.busTransfer(sPoint, ePoint, city, sort);
                } else {
                    String origCity = mapbarService.getCityCode(sPoint);
                    String destCity = mapbarService.getCityCode(ePoint);
                    if (origCity.equals(destCity)) {
                        response = mapbarService.busTransfer(sPoint, ePoint, origCity, sort);
                    } else {
                        response = mapbarService.busCrossCityTransfer(sPoint, ePoint, origCity, destCity);
                    }
                }
            } else {
                // strategy
                // 0 时间优先
                // 1 换乘次数优先
                // 2 地铁优先
                // 3 步行距离优先
                response = searchService.busTransfer(token, adCode, strategy, shpInfoFlag, sPoint, ePoint,
                        showWalkInfo);
            }

            JSONObject resJson = JSONObject.parseObject(response);
            messageBean.setErrcode(0);
            messageBean.setErrmsg(null);
            messageBean.setData(resJson);

        } catch (Exception e) {
            e.printStackTrace();
            messageBean.setErrcode(-1);
            messageBean.setErrmsg("busTransfer error");
        }
        return messageBean;
    }

    /**
     * 公交线路关键字查询
     * <p>
     * 新接口{@link SearchController#busRoute2(java.lang.String, java.lang.String, java.lang.String, java.lang.Boolean, java.lang.Integer, java.lang.Integer)}
     */
    @Deprecated
    @RequestMapping(value = {"/route/busRoute", "/route2/busRoute"})
    public MessageBean busRoute(@RequestParam("adCode") String adCode,
                                @RequestParam(value = "keyword") String keyword) {

        MessageBean messageBean = new MessageBean();

        String response;

        try {
            String city = searchService.getAdNameByAdCode(adCode);
            response = mapbarService.busRoute(keyword, city);

            JSONObject resJson = JSONObject.parseObject(response);
            messageBean.setErrcode(0);
            messageBean.setErrmsg(null);
            messageBean.setData(resJson);

        } catch (Exception e) {
            e.printStackTrace();
            messageBean.setErrcode(-1);
            messageBean.setErrmsg("busRoute error");
        }
        return messageBean;
    }

    /**
     * 公交站点关键字查询
     * <p>
     * 新接口{@link SearchController#busStation2(java.lang.String, java.lang.String, java.lang.Integer, java.lang.Integer)}
     */
    @Deprecated
    @RequestMapping(value = {"/route/busStation", "/route2/busStation"})
    public MessageBean busStation(@RequestParam("adCode") String adCode,
                                  @RequestParam(value = "keyword") String keyword) {

        MessageBean messageBean = new MessageBean();

        String response;

        try {
            String city = searchService.getAdNameByAdCode(adCode);
            response = mapbarService.busStation(keyword, city);

            JSONObject resJson = JSONObject.parseObject(response);
            messageBean.setErrcode(0);
            messageBean.setErrmsg(null);
            messageBean.setData(resJson);

        } catch (Exception e) {
            e.printStackTrace();
            messageBean.setErrcode(-1);
            messageBean.setErrmsg("busStation error");
        }
        return messageBean;
    }

    @RequestMapping(value = {"/search/adminByPoint", "/search2/adminByPoint"})
    public MessageBean adminByPoint(@RequestParam("token") String token,
                                    @RequestParam("location") String location,
                                    @RequestParam(value = "geometry", defaultValue = "0") Integer geometry,
                                    @RequestParam(value = "source", defaultValue = "1") Integer source,
                                    @RequestParam(value = "language", defaultValue = "0") Integer language) {

        MessageBean messageBean = new MessageBean();

        double lon = Double.parseDouble(location.split(",")[0]);
        double lat = Double.parseDouble(location.split(",")[1]);

        String response;

        try {
            if (source == 3) {
                response = mapbarService.adminByPoint(lon, lat);
            } else {
                response = searchService.adminByPoint(token, lon, lat, geometry, language);
            }
            JSONObject resJson = JSONObject.parseObject(response);

            messageBean.setErrcode(0);
            messageBean.setErrmsg(null);
            messageBean.setData(resJson);

        } catch (Exception e) {
            e.printStackTrace();
            messageBean.setErrcode(-1);
            messageBean.setErrmsg("admin by point error");
        }
        return messageBean;
    }

    /**
     * 地理编码 POI 查询
     * <p>
     * 新接口{@link SearchController#geocoding2(java.lang.String, java.lang.String, java.lang.Integer, java.lang.Integer)}
     */
    @Deprecated
    @RequestMapping(value = {"/coder/geocoding", "/coder2/geocoding"})
    public MessageBean geocoding(@RequestParam("token") String token,
                                 @RequestParam("address") String address, @RequestParam("adCode") String adCode,
                                 @RequestParam(value = "source", defaultValue = "1") Integer source) {

        MessageBean messageBean = new MessageBean();

        String response;

        try {
            if (source == 3) {
                response = mapbarService.geocoding(address, adCode);
            } else {
                response = searchService.geocoding(token, address, adCode);
            }

            JSONObject resJson = JSONObject.parseObject(response);

            messageBean.setErrcode(0);
            messageBean.setErrmsg(null);
            messageBean.setData(resJson);

        } catch (Exception e) {
            e.printStackTrace();
            messageBean.setErrcode(-1);
            messageBean.setErrmsg("geocoding error");
        }
        return messageBean;
    }

    /**
     * 地理编码评分
     * <p>
     * 新接口{@link SearchController#geocodingScore2(java.lang.String, java.lang.String)}
     */
    @Deprecated
    @RequestMapping(value = {"/coder/geocoding/score", "/coder2/geocoding/score"})
    public MessageBean geocodingScore(@RequestParam("token") String token,
                                      @RequestParam("address") String address,
                                      @RequestParam(value = "adCode", defaultValue = "") String adCode) {
        MessageBean messageBean = new MessageBean();
        String response;
        try {
            response = mapbarService.geocodingScore(address, adCode);
            JSONObject resJson = JSONObject.parseObject(response);
            messageBean.setErrcode(0);
            messageBean.setErrmsg(null);
            messageBean.setData(resJson);
        } catch (Exception e) {
            e.printStackTrace();
            messageBean.setErrcode(-1);
            messageBean.setErrmsg("geocoding error");
        }
        return messageBean;
    }

    /**
     * 逆地理编码查询
     * <p>
     * 新接口{@link SearchController#reverseGeocoding2(java.lang.String, java.lang.Integer, java.lang.Integer, java.lang.Integer)}
     */
    @Deprecated
    @RequestMapping(value = {"/coder/reverseGeocoding", "/coder2/reverseGeocoding"})
    public MessageBean reverseGeocoding(@RequestParam("token") String token,
                                        @RequestParam("location") String location,
                                        @RequestParam(value = "type", defaultValue = "1") Integer type,
                                        @RequestParam(value = "radius", defaultValue = "300") Integer radius,
                                        @RequestParam(value = "roadRadius", defaultValue = "300") Integer roadRadius,
                                        @RequestParam(value = "kind", defaultValue = "0") Integer kind,
                                        @RequestParam(value = "source", defaultValue = "1") Integer source) {

        MessageBean messageBean = new MessageBean();

        double lon = Double.parseDouble(location.split(",")[0]);
        double lat = Double.parseDouble(location.split(",")[1]);

        String response;

        try {
            if (source == 3) {
                response = mapbarService.reverseGeocoding(lon, lat);
            } else {
                response = searchService.reverseGeocoding(token, lon, lat, type, radius, roadRadius, kind);
            }

            JSONObject resJson = JSONObject.parseObject(response);

            messageBean.setErrcode(0);
            messageBean.setErrmsg(null);
            messageBean.setData(resJson);

        } catch (Exception e) {
            e.printStackTrace();
            messageBean.setErrcode(-1);
            messageBean.setErrmsg("geocoding error");
        }
        return messageBean;
    }

    @RequestMapping(value = {"/search/around", "/search2/around"})
    public MessageBean around(@RequestParam("token") String token,
                              @RequestParam("location") String location, @RequestParam("type") String type,
                              @RequestParam(value = "radius", defaultValue = "1000") Integer radius,
                              @RequestParam(value = "count", defaultValue = "5") Integer count) {

        MessageBean messageBean = new MessageBean();

        double lon = Double.parseDouble(location.split(",")[0]);
        double lat = Double.parseDouble(location.split(",")[1]);

        String response;

        try {
            response = searchService.around(token, lon, lat, type, radius, count);
            JSONObject resJson = JSONObject.parseObject(response);

            messageBean.setErrcode(0);
            messageBean.setErrmsg(null);
            messageBean.setData(resJson);

        } catch (Exception e) {
            e.printStackTrace();
            messageBean.setErrcode(-1);
            messageBean.setErrmsg("around error");
        }
        return messageBean;
    }

    @RequestMapping(value = {"/tips/smartTips", "/tips2/smartTips"})
    public MessageBean smartTips(@RequestParam("token") String token, @RequestParam("input") String input,
                                 @RequestParam("adCode") int adCode,
                                 @RequestParam(value = "searchType", defaultValue = "all") String searchType,
                                 @RequestParam(value = "pageCount", defaultValue = "10") Integer pageCount) {
        MessageBean messageBean = new MessageBean();
        try {
            String response = searchService.smartTips(token, input, adCode, searchType, pageCount);
            JSONObject resJson = JSONObject.parseObject(response);

            messageBean.setErrcode(0);
            messageBean.setErrmsg(null);
            messageBean.setData(resJson);
        } catch (Exception e) {
            e.printStackTrace();
            messageBean.setErrcode(-1);
            messageBean.setErrmsg("smartTips error");
        }

        return messageBean;
    }


    @RequestMapping(value = {"/search/suggest", "/search2/suggest"})
    public MessageBean suggest(@RequestParam("keywords") String keywords, @RequestParam("city") String city) {
        MessageBean messageBean = new MessageBean();
        try {
            String response = mapbarService.suggest(keywords, city);
            JSONObject resJson = JSONObject.parseObject(response);
            messageBean.setErrcode(0);
            messageBean.setErrmsg(null);
            messageBean.setData(resJson);
        } catch (Exception e) {
            e.printStackTrace();
            messageBean.setErrcode(-1);
            messageBean.setErrmsg("suggest error");
        }

        return messageBean;
    }

    /* --------------------------------------------- minemap 接口调整 ------------------------------------------------ */

    /**
     * 一框搜索(新)
     */
    @ApiOperation(value = "一框搜索(新)", notes = "一框搜（关键字、周边搜）")
    @RequestMapping("/search/keyword2")
    public MessageBean keywordSearch(@ApiParam("adcode/城市名称") @RequestParam(value = "city", defaultValue = "") String city,
                                     @ApiParam("关键字") @RequestParam("keywords") String keywords,
                                     @ApiParam("关键字搜索:搜索指定位置，若area和zoom为空，则默认为该位置所在城市;周边搜索:指定位置，和range配合使用") @RequestParam("location") String location,
                                     @ApiParam("(关键字搜索使用)当前视野的经纬度坐标（02坐标）") @RequestParam(value = "area", required = false) String area,
                                     @ApiParam("(周边搜索使用)周边搜索指定范围") @RequestParam(value = "range", defaultValue = "3000") String range,
                                     @ApiParam("(关键字搜索使用)缩放级别") @RequestParam(value = "zoom", defaultValue = "12") Integer zoom,
                                     @ApiParam("搜索类型") @RequestParam(value = "search_type", defaultValue = "normal") String searchType,
                                     @ApiParam("语言") @RequestParam(value = "language", defaultValue = "zh_CN") String language,
                                     @ApiParam("当前页数") @RequestParam(value = "page_num", defaultValue = "1") Integer pageNum,
                                     @ApiParam("每页记录数") @RequestParam(value = "page_size", defaultValue = "10") Integer pageSize) {

        MessageBean messageBean = new MessageBean();
        String response;
        try {
            if ("for_around".equals(searchType)) {// 强制进行周边搜索
                response = mapbarService.keywordAround(keywords, city, location, range, pageNum, pageSize, language, searchType);
            } else {
                response = mapbarService.keywordSearch(keywords, city, location, area, zoom, pageNum, pageSize, language, searchType);
            }

            JSONObject resJson = JSONObject.parseObject(response);
            messageBean.setErrcode(0);
            messageBean.setErrmsg(null);
            messageBean.setData(resJson);
        } catch (Exception e) {
            e.printStackTrace();
            messageBean.setErrcode(-1);
            messageBean.setErrmsg("geocoding error");
        }
        return messageBean;
    }

    /**
     * 地理编码评分(新)
     * <p>
     * 原接口{@link SearchController#geocodingScore(java.lang.String, java.lang.String, java.lang.String)}
     */
    @ApiOperation(value = "地理编码评分")
    @RequestMapping("/coder/geocoding/score2")
    public MessageBean geocodingScore2(@ApiParam("关键字") @RequestParam("keywords") String keywords,
                                       @ApiParam("adcode/城市名称") @RequestParam("city") String city) {
        MessageBean messageBean = new MessageBean();
        String response;
        try {
            response = mapbarService.geocodingScore2(keywords, city);
            JSONObject resJson = JSONObject.parseObject(response);
            messageBean.setErrcode(0);
            messageBean.setErrmsg(null);
            messageBean.setData(resJson);
        } catch (Exception e) {
            e.printStackTrace();
            messageBean.setErrcode(-1);
            messageBean.setErrmsg("geocoding error");
        }
        return messageBean;
    }

    /**
     * 路线规划(新)
     * <p>
     * 原接口{@link SearchController#newDriving(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.Integer, java.lang.String)}
     */
    @ApiOperation(value = "路线规划")
    @RequestMapping("/route/driving3")
    public MessageBean newDriving2(@ApiParam("起点坐标") @RequestParam("sPoint") String sPoint,
                                   @ApiParam("终点坐标") @RequestParam("ePoint") String ePoint,
                                   @ApiParam("算路模式") @RequestParam(value = "st", defaultValue = "4", required = false) Integer st,
                                   @ApiParam("途经点") @RequestParam(value = "wayPoints", defaultValue = "", required = false) String wayPoints,
                                   @ApiParam("避让区域") @RequestParam(value = "avoidArea", defaultValue = "", required = false) String avoidArea) {

        MessageBean messageBean = new MessageBean();

        String response;

        try {
            /* st: 0 高速优先、1 最短距离、2 避让高速、3 步行模式、4 推荐、5 经济、6 避让拥堵 */
            response = mapbarService.newDriving2(sPoint, ePoint, wayPoints, st, avoidArea);
            JSONObject resJson = JSONObject.parseObject(response);
            messageBean.setErrcode(0);
            messageBean.setErrmsg(null);
            messageBean.setData(resJson);

        } catch (Exception e) {
            e.printStackTrace();
            messageBean.setErrcode(-1);
            messageBean.setErrmsg("driving error");
        }
        return messageBean;
    }

    /**
     * 货车路线规划
     */
    @ApiOperation(value = "货车路线规划")
    @RequestMapping("/route/truck-driving")
    public MessageBean truckDriving(@ApiParam("起点坐标") @RequestParam("sPoint") String sPoint,
                                    @ApiParam("终点坐标") @RequestParam("ePoint") String ePoint,
                                    @ApiParam("算路模式") @RequestParam(value = "st", defaultValue = "4", required = false) Integer st,
                                    @ApiParam("途经点") @RequestParam(value = "wayPoints", defaultValue = "", required = false) String wayPoints,
                                    @ApiParam("避让区域") @RequestParam(value = "avoidArea", defaultValue = "", required = false) String avoidArea,
                                    @ApiParam("长度") @RequestParam(value = "length", required = false) Double length,
                                    @ApiParam("高度") @RequestParam(value = "height", required = false) Double height,
                                    @ApiParam("宽度") @RequestParam(value = "width", required = false) Double width,
                                    @ApiParam("货车重量") @RequestParam(value = "weight", required = false) Integer weight,
                                    @ApiParam("轴重") @RequestParam(value = "axleWeight", required = false) Integer axleWeight,
                                    @ApiParam("轴数") @RequestParam(value = "axleCount", required = false) Integer axleCount,
                                    @ApiParam("是否拥有通行证") @RequestParam(value = "hasPassport", defaultValue = "true", required = false) Boolean hasPassport,
                                    @ApiParam("车辆能源类型") @RequestParam(value = "energyType", required = false) Integer energyType,
                                    @ApiParam("机动车污染物排放标准") @RequestParam(value = "emissionStandard", required = false) Integer emissionStandard,
                                    @ApiParam("货车负载类型") @RequestParam(value = "truckLoadType", required = false) Integer truckLoadType,
                                    @ApiParam("货车类型") @RequestParam(value = "truckType", required = false) Integer truckType,
                                    @ApiParam("货车功能") @RequestParam(value = "truckFunction", required = false) Integer truckFunction,
                                    @ApiParam("车牌号") @RequestParam(value = "plateNumber", required = false) Integer plateNumber) {

        MessageBean messageBean = new MessageBean();

        String response;

        try {
            JSONObject vehicleInfo = new JSONObject();
            vehicleInfo.put("mainKind", 1); //设置车辆类型为 1:货车
            if (length != null) vehicleInfo.put("length", length);
            if (height != null) vehicleInfo.put("height", height);
            if (width != null) vehicleInfo.put("width", width);
            if (weight != null) vehicleInfo.put("weight", weight);
            if (axleWeight != null) vehicleInfo.put("axleWeight", axleWeight);
            if (axleCount != null) vehicleInfo.put("axleCount", axleCount);
            if (hasPassport != null) vehicleInfo.put("hasPassport", hasPassport);
            if (energyType != null) vehicleInfo.put("energyType", energyType);
            if (emissionStandard != null) vehicleInfo.put("emissionStandard", emissionStandard);
            if (truckLoadType != null) vehicleInfo.put("truckLoadType", truckLoadType);
            if (truckType != null) vehicleInfo.put("truckType", truckType);
            if (truckFunction != null) vehicleInfo.put("truckFunction", truckFunction);
            if (plateNumber != null) vehicleInfo.put("plateNumber", plateNumber);

            JSONObject vehicle = new JSONObject();
            vehicle.put("vehicleInfo", vehicleInfo);

            /* st: 0 高速优先、1 最短距离、2 避让高速、3 步行模式、4 推荐、5 经济、6 避让拥堵 */
            response = mapbarService.truckDriving(sPoint, ePoint, wayPoints, st, avoidArea, vehicle.toJSONString());
            JSONObject resJson = JSONObject.parseObject(response);
            messageBean.setErrcode(0);
            messageBean.setErrmsg(null);
            messageBean.setData(resJson);

        } catch (Exception e) {
            e.printStackTrace();
            messageBean.setErrcode(-1);
            messageBean.setErrmsg("driving error");
        }
        return messageBean;
    }

    /**
     * 公交换乘-经纬度查询(新)
     * <p>
     * 原接口{@link SearchController#busTransfer(java.lang.String, java.lang.String, java.lang.Integer, java.lang.Integer, java.lang.String, java.lang.String, java.lang.Integer, java.lang.Integer)}
     */
    @ApiOperation(value = "公交换乘-经纬度查询")
    @RequestMapping("/route/bus-transfer")
    public MessageBean busTransfer2(@ApiParam("行政区划编码或城市名称") @RequestParam(value = "city", defaultValue = "") String city,
                                    @ApiParam("起点坐标") @RequestParam("sPoint") String sPoint,
                                    @ApiParam("终点坐标") @RequestParam("ePoint") String ePoint,
                                    @ApiParam("换乘策略") @RequestParam(value = "strategy", defaultValue = "0") Integer strategy) {

        MessageBean messageBean = new MessageBean();

        String response;

        try {
            String sort = "subway";
            if (city != null && !"".equals(city)) {
                /* "换乘策略: 0-最快到达、1-最少换乘、2-只换乘地铁、3-最少步行、4-不换乘地铁" */
                if (strategy == 0) {
                    sort = "subway";
                } else if (strategy == 1) {
                    sort = "nonstop";
                } else if (strategy == 2) {
                    sort = "subway";
                } else if (strategy == 3) {
                    sort = "shortWalk";
                } else {
                    sort = "unsubway";
                }

                String cityName = searchService.getAdNameByAdCode(city);
                response = mapbarService.busTransfer2(sPoint, ePoint, cityName, sort);
            } else {
                String origCity = mapbarService.getCityCode(sPoint);
                String destCity = mapbarService.getCityCode(ePoint);
                if (origCity.equals(destCity)) {
                    response = mapbarService.busTransfer2(sPoint, ePoint, origCity, sort);
                } else {
                    response = mapbarService.busCrossCityTransfer(sPoint, ePoint, origCity, destCity);
                }
            }

            JSONObject resJson = JSONObject.parseObject(response);
            messageBean.setErrcode(0);
            messageBean.setErrmsg(null);
            messageBean.setData(resJson);

        } catch (Exception e) {
            e.printStackTrace();
            messageBean.setErrcode(-1);
            messageBean.setErrmsg("busTransfer error");
        }
        return messageBean;
    }

    /**
     * 公交站点关键字查询
     * <p>
     * 原接口{@link SearchController#busStation(java.lang.String, java.lang.String)}
     */
    @ApiOperation(value = "公交站点关键字查询")
    @RequestMapping("/route/bus-station")
    public MessageBean busStation2(@ApiParam("关键字") @RequestParam(value = "keyword") String keyword,
                                   @ApiParam("城市名称") @RequestParam("city") String city,
                                   @ApiParam("当前页数") @RequestParam(name = "pn", defaultValue = "1") Integer pn,
                                   @ApiParam("每页显示数") @RequestParam(name = "rn", defaultValue = "20") Integer rn) {

        MessageBean messageBean = new MessageBean();

        String response;

        try {
            String cityName = searchService.getAdNameByAdCode(city);
            response = mapbarService.busStation2(keyword, cityName, pn, rn);

            JSONObject resJson = JSONObject.parseObject(response);
            messageBean.setErrcode(0);
            messageBean.setErrmsg(null);
            messageBean.setData(resJson);

        } catch (Exception e) {
            e.printStackTrace();
            messageBean.setErrcode(-1);
            messageBean.setErrmsg("busStation error");
        }
        return messageBean;
    }

    /**
     * 公交线路关键字查询
     * <p>
     * 原接口{@link SearchController#busRoute(java.lang.String, java.lang.String)}
     */
    @ApiOperation(value = "公交线路关键字查询")
    @RequestMapping("/route/bus-route")
    public MessageBean busRoute2(@ApiParam("城市名称") @RequestParam("city") String city,
                                 @ApiParam("关键字") @RequestParam("keyword") String keyword,
                                 @ApiParam("查询方式") @RequestParam(name = "type", defaultValue = "name") String type,
                                 @ApiParam("是否显示返程线路信息") @RequestParam(name = "opposite", defaultValue = "false") Boolean opposite,
                                 @ApiParam("当前页数") @RequestParam(name = "pn", defaultValue = "1") Integer pn,
                                 @ApiParam("每页显示数") @RequestParam(name = "rn", defaultValue = "20") Integer rn) {

        MessageBean messageBean = new MessageBean();

        String response;

        try {
            String cityName = searchService.getAdNameByAdCode(city);
            response = mapbarService.busRoute2(keyword, cityName, type, opposite, pn, rn);

            JSONObject resJson = JSONObject.parseObject(response);
            messageBean.setErrcode(0);
            messageBean.setErrmsg(null);
            messageBean.setData(resJson);

        } catch (Exception e) {
            e.printStackTrace();
            messageBean.setErrcode(-1);
            messageBean.setErrmsg("busRoute error");
        }
        return messageBean;
    }

    /**
     * 地理编码 POI 查询(新)
     * <p>
     * 原接口{@link SearchController#geocoding(java.lang.String, java.lang.String, java.lang.String, java.lang.Integer)}
     */
    @ApiOperation(value = "地理编码 POI 查询(新)")
    @RequestMapping("/coder/geocoding2")
    public MessageBean geocoding2(@ApiParam("关键字") @RequestParam("keywords") String keywords,
                                  @ApiParam("adcode/城市名称") @RequestParam("city") String city,
                                  @ApiParam("当前页数") @RequestParam(value = "page_num", defaultValue = "1") Integer page_num,
                                  @ApiParam("每页记录数") @RequestParam(value = "page_size", defaultValue = "10") Integer page_size) {

        MessageBean messageBean = new MessageBean();

        String response;

        try {
            response = mapbarService.geocoding2(keywords, city, page_num, page_size);

            JSONObject resJson = JSONObject.parseObject(response);

            messageBean.setErrcode(0);
            messageBean.setErrmsg(null);
            messageBean.setData(resJson);

        } catch (Exception e) {
            e.printStackTrace();
            messageBean.setErrcode(-1);
            messageBean.setErrmsg("geocoding error");
        }
        return messageBean;
    }

    /**
     * 逆地理编码查询(新)
     * <p>
     * 原接口{@link SearchController#reverseGeocoding(java.lang.String, java.lang.String, java.lang.Integer, java.lang.Integer, java.lang.Integer, java.lang.Integer, java.lang.Integer)}
     */
    @ApiOperation(value = "逆地理编码查询(新)")
    @RequestMapping("/coder/reverse-geocoding")
    public MessageBean reverseGeocoding2(@ApiParam("经纬度") @RequestParam("location") String location,
                                         @ApiParam("是否显示带有地标点的完整详细信息详细信息") @RequestParam(value = "detail", defaultValue = "0") Integer detail,
                                         @ApiParam("是否显示道路信息") @RequestParam(value = "road", defaultValue = "0") Integer road,
                                         @ApiParam("返回数量") @RequestParam(value = "rowNumber", defaultValue = "0") Integer rowNumber) {

        MessageBean messageBean = new MessageBean();

        double lon = Double.parseDouble(location.split(",")[0]);
        double lat = Double.parseDouble(location.split(",")[1]);

        String response;

        try {
            response = mapbarService.reverseGeocoding2(lon, lat, detail, road, rowNumber);

            JSONObject resJson = JSONObject.parseObject(response);

            messageBean.setErrcode(0);
            messageBean.setErrmsg(null);
            messageBean.setData(resJson);

        } catch (Exception e) {
            e.printStackTrace();
            messageBean.setErrcode(-1);
            messageBean.setErrmsg("geocoding error");
        }
        return messageBean;
    }

    /**
     * 行政区信息查询(新)
     */
    @ApiOperation(value = "行政区信息查询", notes = "包括行政区中心点及行政区边界信息查询")
    @RequestMapping("/search/admin")
    public MessageBean adminSearch(@ApiParam("行政区名称") @RequestParam("keywords") String keywords,
                                   @ApiParam("搜索指定位置(默认坐标为北京市中心点坐标)")
                                   @RequestParam(name = "location", defaultValue = "") String location) {

        MessageBean messageBean = new MessageBean();
        String response;
        try {
            response = mapbarService.adminSearch(keywords, location);

            JSONObject resJson = JSONObject.parseObject(response);
            messageBean.setErrcode(0);
            messageBean.setErrmsg(null);
            messageBean.setData(resJson);
        } catch (Exception e) {
            e.printStackTrace();
            messageBean.setErrcode(-1);
            messageBean.setErrmsg("geocoding error");
        }
        return messageBean;
    }

}
